import player from '@/models/Player'
import system from '@/models/system'

export const checkAlive = () => {
  if (player.hp > 0) {
    return true
  } else {
    return false
  }
}

export const checkWinGame = () => {
  if (player.buriedTotem && player.hp > 0) {
    return true
  } else {
    return false
  }
}

export const checkItems = () => {
  for (let index = player.items.length - 1; index >= 0; index--) {
    const item = player.items[index]
    if (item.useTime <= 0 && item.type !== 'weapon') {
      player.items.splice(index, 1)
    }
  }
}

export const hasValidEntrance = () => {
  let hasFlag = false
  for (let index = 0; index < system.map.length; index++) {
    const r = system.map[index]
    const realDoorIndex = r.doors.map(i => {
      return (i + r.rotate / 90) % 4
    })
    const up = system.map.findIndex(tr => tr.x === r.x && tr.y === r.y - 1)
    if (up === -1 && realDoorIndex.includes(0)) {
      hasFlag = true
      break
    }
    const down = system.map.findIndex(tr => tr.x === r.x && tr.y === r.y + 1)
    if (down === -1 && realDoorIndex.includes(2)) {
      hasFlag = true
      break
    }
    const left = system.map.findIndex(tr => tr.x === r.x - 1 && tr.y === r.y)
    if (left === -1 && realDoorIndex.includes(3)) {
      hasFlag = true
      break
    }
    const right = system.map.findIndex(tr => tr.x === r.x + 1 && tr.y === r.y)
    if (right === -1 && realDoorIndex.includes(1)) {
      hasFlag = true
      break
    }
  }
  return hasFlag
}

export const getBreakInEntrance = () => {
  const currentRoom = system.map.find(r => r.x === player.x && r.y === player.y)
  const realDoorIndex = currentRoom.doors.map(i => {
    return (i + currentRoom.rotate / 90) % 4
  })
  const up = system.map.findIndex(tr => tr.x === currentRoom.x && tr.y === currentRoom.y - 1)
  if (up === -1 && !realDoorIndex.includes(0)) {
    return (0 + 4 - currentRoom.rotate / 90) % 4
  }
  const down = system.map.findIndex(tr => tr.x === currentRoom.x && tr.y === currentRoom.y + 1)
  if (down === -1 && !realDoorIndex.includes(2)) {
    return (2 + 4 - currentRoom.rotate / 90) % 4
  }
  const left = system.map.findIndex(tr => tr.x === currentRoom.x - 1 && tr.y === currentRoom.y)
  if (left === -1 && !realDoorIndex.includes(3)) {
    return (3 + 4 - currentRoom.rotate / 90) % 4
  }
  const right = system.map.findIndex(tr => tr.x === currentRoom.x + 1 && tr.y === currentRoom.y)
  if (right === -1 && !realDoorIndex.includes(1)) {
    return (1 + 4 - currentRoom.rotate / 90) % 4
  }
  return -1
}

export const getVisitedRooms = () => {
  let count = 0
  const upRoom = system.map.find(r => r.x === player.x && r.y === player.y - 1)
  const downRoom = system.map.find(r => r.x === player.x && r.y === player.y + 1)
  const leftRoom = system.map.find(r => r.x === player.x - 1 && r.y === player.y)
  const rightRoom = system.map.find(r => r.x === player.x + 1 && r.y === player.y)
  upRoom && count++
  downRoom && count++
  leftRoom && count++
  rightRoom && count++
  return {
    up: upRoom,
    down: downRoom,
    left: leftRoom,
    right: rightRoom,
    count
  }
}
